// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 * TEMPLATE:    SpringServiceImpl.vsl in andromda-spring cartridge
 * MODEL CLASS: carmanagement::com.volkwagen.management::service::CarManagementService
 * STEREOTYPE:  Service
 */
package com.volkwagen.management.service;

import com.volkwagen.management.domain.Car;
import com.volkwagen.management.domain.CarDao;
import com.volkwagen.management.domain.Status;
import com.volkwagen.management.domain.User;
import com.volkwagen.management.vo.CarSearchCriteriaVO;
import com.volkwagen.management.vo.CarVO;
import com.volkwagen.management.vo.NewCarVO;
import java.util.ArrayList;
import java.util.List;

/**
 * @see com.volkwagen.management.service.CarManagementService
 */
public class CarManagementServiceImpl
        extends CarManagementServiceBase {

    /**
     * @see com.volkwagen.management.service.CarManagementService#getAllCars()
     */
    protected List handleGetAllCars()
            throws Exception {
        return new ArrayList(this.getCarDao().loadAll(CarDao.TRANSFORM_DETAILCARVO));
    }

    /**
     * @see com.volkwagen.management.service.CarManagementService#createCar(NewCarVO)
     */
    protected CarVO handleCreateCar(NewCarVO newCarVo)
            throws Exception {
        User user = this.getUserDao().get(newCarVo.getUserId());
        Car car = this.getCarDao().newCarVOToEntity(newCarVo);
        car.setUser(user);
        return (CarVO) this.getCarDao().create(CarDao.TRANSFORM_CARVO, car);
    }

    /**
     * @see com.volkwagen.management.service.CarManagementService#finCars(CarSearchCriteriaVO)
     */
    protected List handleFinCars(CarSearchCriteriaVO criteria)
            throws Exception {
        return this.getCarDao().findByCriteria(criteria);
    }

    /**
     * @see com.volkwagen.management.service.CarManagementService#findCar(Long)
     */
    protected CarVO handleFindCar(Long id)
            throws Exception {
        return (CarVO) this.getCarDao().get(CarDao.TRANSFORM_CARVO, id);
    }

    /**
     * @see com.volkwagen.management.service.CarManagementService#updateCar(CarVO)
     */
    protected void handleUpdateCar(CarVO updateCarVO)
            throws Exception {
        Car car = this.getCarDao().get(updateCarVO.getId());
        if (car != null) {
            car.setColor(updateCarVO.getColor());
            car.setModel(updateCarVO.getModel());
            car.setNumberOfDoors(updateCarVO.getNumberOfDoors());
            car.setNumberOfWheels(updateCarVO.getNumberOfDoors());
            car.setStatus((Status.fromString(updateCarVO.getStatus())));
            car.setUser(getUserDao().get(updateCarVO.getUserId()));
        }
        this.getCarDao().update(car);
    }
}